package com.smarth.zhizulian.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.smarth.zhizulian.R;
import com.smarth.zhizulian.activity.MainActivity;
import com.smarth.zhizulian.activity.MoneyDetailActivity;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.app.BaseFragment;
import com.smarth.zhizulian.app.EventFragment;
import com.smarth.zhizulian.bean.Page;
import com.smarth.zhizulian.bean.message.MessageAppNews;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.utils.listhelper.RefreshHelper;
import com.smarth.zhizulian.utils.listhelper.impl.adapter.RListDataAdapter;
import com.smarth.zhizulian.utils.listhelper.impl.adapter.RMultiListDataAdapter;
import com.smarth.zhizulian.utils.listhelper.impl.model.HttpListDataModel;
import com.smarth.zhizulian.utils.listhelper.inter.ResponseSender;
import com.smarth.zhizulian.view.LoadingLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * Created by 18865 on 2018/2/28.
 */

public class MessageAppFragment extends EventFragment {
    @BindView(R.id.rc_recyclerview)
    RecyclerView rcRecyclerview;
    @BindView(R.id.rv_refresh)
    SmartRefreshLayout rvRefresh;
    @BindView(R.id.ll_loadinglayout)
    LoadingLayout llLoadinglayout;
    private RefreshHelper<List<MessageAppNews>> refreshHelper;


    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setBKView(R.layout.global_fragment_rc);
        initListView();
    }

    private void initListView() {
        rcRecyclerview.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        refreshHelper = new RefreshHelper<>(mContext, llLoadinglayout, rvRefresh);
        refreshHelper.setDataModel(new HttpListDataModel<List<MessageAppNews>>(mContext) {
            @Override
            public Observable<? extends Page> getObservable(ResponseSender<List<MessageAppNews>> responseSender, int page) {
                return KumaHttp.getService().messageAppNews(page).compose(RxCompos.<Page<MessageAppNews>>getBELoginCompos());
            }
        });
        MyAdapter adapter = new MyAdapter(new ArrayList<MessageAppNews>());
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //1 新手红包 2 新手任务 3 每日幸运红包 4 升级提示 5 降级提示 6 收徒提醒 7 账户清零提醒 8 徒弟首次提现成功奖励提醒 9 提现成功 10 提现失败
                switch (((MessageAppNews) adapter.getData().get(position)).getMessageAppId()) {
                    case 1:
                        MoneyDetailActivity.open(mContext);
                        break;
                    case 2:
                        MoneyDetailActivity.open(mContext);
                        break;
                    case 3:
                        MoneyDetailActivity.open(mContext);
                        break;
                    case 6:
                        MainActivity.openClearTask(mContext, MainActivity.MODE_DISCIPLE);
                        break;
                    case 8:
                        MoneyDetailActivity.open(mContext);
                        break;
                    case 9:
                        MoneyDetailActivity.openForGetMoney(mContext);
                        break;
                    case 10:
                        MoneyDetailActivity.openForGetMoney(mContext);
                        break;
                }
            }
        });
        refreshHelper.setDataAdapter(adapter);
        refreshHelper.refresh();
    }

    public class MyAdapter extends RMultiListDataAdapter<MessageAppNews> {

        public MyAdapter(@Nullable List<MessageAppNews> data) {
            super(data);
            addItemType(MessageAppNews.TYPE_LOOKMORE, R.layout.item_message_lookmore);
            addItemType(MessageAppNews.TYPE_NOMORE, R.layout.item_message_nomore);
        }

        @Override
        protected void convert(BaseViewHolder helper, MessageAppNews item) {
            switch (helper.getItemViewType()) {
                case MessageAppNews.TYPE_LOOKMORE:
                case MessageAppNews.TYPE_NOMORE:
                    helper.setText(R.id.tv_time, item.getCreateTime());
                    helper.setText(R.id.tv_title, item.getTitle());
                    helper.setText(R.id.tv_content, item.getContent());
                    break;
            }
        }
    }

    @Subscriber(tag= BaseConfig.EventTag.ON_CLEAR_MESSAGE)
    private void onEventClearMessage(EventBean eb) {
        refreshHelper.refresh();
    }
}
